/**
 * 实现一个死锁的场景
 * @author TANGYE
 * @2021/3/2 16:29
 */
public class DeadLock {
    private static Object lock1 = new Object();
    private static Object lock2 = new Object();

    public void methodOne(){
        System.out.println("methodOne");
        synchronized (lock1){
            try {
                Thread.sleep(200);
                methodTwo();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("leave methodOne");
    }
    public void methodTwo(){
        System.out.println("methodTwo");
        synchronized (lock2){
            try {
                Thread.sleep(500);
                methodOne();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("leave methodTwo");
    }

    public static void main(String[] args) {
        DeadLock deadLock = new DeadLock();
        Thread thread1 = new Thread(() -> deadLock.methodOne());
        Thread thread2 = new Thread(() -> deadLock.methodTwo());
        thread1.start();
        thread2.start();
    }
}
